﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

namespace Code.UI
{
    public class SkillSlot:MonoBehaviour
    {
        public Action<int,Skill> OnSelect;

        public int Index;
         
        private Sprite _orignImg;
        [SerializeField] private Image _skillImg;
        [SerializeField] private GameObject _imgMask;

        private Skill _currentSkill;
        private void Awake()
        {
            _orignImg = GetComponent<Image>().sprite;
        }

        public void Init(Skill skill,UIPlayer player)
        {
            if (_currentSkill == null&&!player.SkillConfigDict.ContainsKey(skill)) 
            {
                player.SkillConfigDict.Add(skill,Index);
                _currentSkill = skill;
                SetUI(skill);   
            }
            else if(_currentSkill!=null && player.SkillConfigDict.ContainsKey(skill)) 
            {
                player.SkillConfigDict.Remove(skill);
                _currentSkill = null;
                Reset();
            }
            //OnSelect?.Invoke(Index, _currentSkill);

        }

        public void SetUI(Skill skill)
        {
            _currentSkill = skill;
            _skillImg.sprite = skill.Img;
        }

        internal void SetMask(bool active)
        {
            _imgMask.SetActive(active);
        }

        internal void Reset()
        {
            GetComponent<Image>().sprite = _orignImg;
        }
    }
}
